from werkzeug.security import generate_password_hash, check_password_hash

from ..exts import db
from . import BaseModel




COMMON_USER = 0
BLACK_USER = 1
VIP_USER = 2

class MovieOrder(BaseModel):
    __tablename__ = 'tb_movie_order'
    user_id = db.Column(db.Integer, db.ForeignKey("tb_movie_user.id"))
    hall_movie_id = db.Column(db.Integer, db.ForeignKey("hall_movie.id"))
    time = db.Column(db.DateTime, comment='下单时间')
    status = db.Column(db.Integer, default=0, comment='订单状态')
    seats = db.Column(db.String(128), comment='座位')
    price = db.Column(db.Float, default=0, comment='订单价格')


class MovieUser(BaseModel):
    __tablename__ = 'tb_movie_user'
    username = db.Column(db.String(32), unique=True, comment='用户名')
    _password = db.Column(db.String(256), comment='用户密码')
    phone = db.Column(db.String(11), unique=True, comment='手机号')
    email = db.Column(db.String(32), unique=True, comment='邮箱')
    is_delete = db.Column(db.Boolean, default=False, comment='逻辑删除')
    permission = db.Column(db.Integer, default=0, comment='用户权限')

    @property
    def password(self):
        raise Exception("Access Not Allowed")

    @password.setter
    def password(self, pwd):
        self._password = generate_password_hash(pwd)

    def check_pwd(self, pwd):
        return check_password_hash(self._password, pwd)

    def check_permission(self, permission):
        if (1 and self.permission) == 1:
            return False
        else:
            return permission and self.permission == permission
